﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using AspNetCs.Bll;
using AspNetCs.Model;
using AspNetCs.Common;
using AspNetCs.Enumerations;

public partial class AncestralHallBBS_BBSIndex :BasePage
{
    public int AncestralHallId = 0;
    public int AncestralHallItemId = 0;
    public int BBSMsgId = 0;
    protected string userName = "";
    protected string crruentUserName = "";

    private AncestralHallBBSMsg_BLL bbsBll = AncestralHallBBSMsgBLL.CreateNew;
    private AncestralHallBBSReplyMsg_BLL replyBll = AncestralHallBBSReplyMsgBLL.CreateNew;
    private AncestralHallBLL ancestralHallBLL = AncestralHall_Bll.CreatNew;
    protected AncestralHallBBSBLL ancestralHallBBSBLL = AncestralHallBBS_BLL.CreateNew;
    protected AncestralHallItemsBLL ancestralHallItemsBLL = AncestralHallItems_BLL.CreateNew;

    private int userId = 0;

    protected AncestralHallBBSReplyMsgModel replyModel = new AncestralHallBBSReplyMsgModel();
    protected AncestralHallBBSMsgModel msgModel = new AncestralHallBBSMsgModel();
    protected AncestralHall_Model ancestralHallModel = new AncestralHall_Model();
    protected AncestralHallBBS_Model ancestralHallBBSModel = new AncestralHallBBS_Model();
    protected AncestralHallItems_Model ancestralHallItemsModel = new AncestralHallItems_Model();

    protected int msnCount = 0;
    protected int todayMsnCount = 0;
    protected int replyMsnCount = 0;


    protected int MsgListCount = 0;

    /// <summary>
    /// 0:表示首页 1:表示论坛版块 2:表示留言详情
    /// </summary>
    public int accessType = 0;


    protected void Page_Load(object sender, EventArgs e)
    {
        //CheckUserExit("../Users/Login.Aspx", "请您登录", true);
        crruentUserName = Base_GetUserInfo.UserName;
        userId = Base_GetUserInfo.UserId;
        initPage();
        if (!IsPostBack)
        {
            
            setPage();
        }

        string cmdText = "select count(ab.id) from AncestralHallBBSMsg as ab inner join AncestralHallItems as al on al.Id=ab.AncestralHallItemId where al.AncestralHallId=" + this.AncestralHallId;
        string cmdTextReplyMsg = "select count(ab.id) from AncestralHallBBSMsg as ab inner join AncestralHallItems as al on al.Id=ab.AncestralHallItemId inner join AncestralHallBBSReplyMsg as ass on ab.id=ass.AncestralHallBBSMsgId where al.AncestralHallId=" + this.AncestralHallId;
        string cmdText2 = "select count(ab.id) from AncestralHallBBSMsg as ab inner join AncestralHallItems as al on al.Id=ab.AncestralHallItemId where datediff(dd,ab.PublishTime,'"+DateTime.Now.ToString()+"')=0 and al.AncestralHallId=" + this.AncestralHallId;
        DataTable dataTable = new DataTable();
        DataTable dataTable1 = new DataTable();
        DataTable dataTable2 = new DataTable();
        dataTable = ExcuteSqlTextBLL.GetDataTable(cmdText);
        dataTable1 = ExcuteSqlTextBLL.GetDataTable(cmdTextReplyMsg);
        dataTable2 = ExcuteSqlTextBLL.GetDataTable(cmdText2);
        msnCount = StringHelper.isNumber_int(dataTable.Rows[0][0].ToString());
        replyMsnCount = StringHelper.isNumber_int(dataTable1.Rows[0][0].ToString());
        todayMsnCount = StringHelper.isNumber_int(dataTable2.Rows[0][0].ToString());
        
    }

    public void initPage()
    {

        
        
        if (!IsPostBack)
        {
            AncestralHallId = StringHelper.isNumber_int(Request["AncestralHallId"]);
            ViewState["AncestralHallId"] = AncestralHallId;
            AncestralHallItemId = StringHelper.isNumber_int(Request["AncestralHallItemId"]);
            ViewState["AncestralHallItemId"] = AncestralHallItemId;
            accessType = StringHelper.isNumber_int(Request["accessType"]);
            ViewState["accessType"] = accessType;
            BBSMsgId = StringHelper.isNumber_int(Request["BBSMsgId"]);
            ViewState["BBSMsgId"] = BBSMsgId;
        }
        else
        {
            if (ViewState["AncestralHallId"]!=null)
            {
                AncestralHallId = StringHelper.isNumber_int(ViewState["AncestralHallId"].ToString());  
            }
            if (ViewState["AncestralHallItemId"] != null)
            {
                AncestralHallItemId = StringHelper.isNumber_int(ViewState["AncestralHallItemId"].ToString());
            }
            if (ViewState["accessType"]!=null)
            {
                accessType = StringHelper.isNumber_int(ViewState["accessType"].ToString());
            }
            if (ViewState["BBSMsgId"]!=null)
            {
                BBSMsgId = StringHelper.isNumber_int(ViewState["BBSMsgId"].ToString());
            }
        }
    }


    public void setPage()
    {
        switch (accessType)
        {
            case 0:
                palIndex.Visible = true;
                palItem.Visible = false;
                palBBsDetail.Visible = false;
                DataBindItem();
                break;
            case 1:
                palIndex.Visible = false;
                palItem.Visible = true;
                palBBsDetail.Visible = false;
                DataBindBBSMsg();
                GetAncestralHallItemModel();
                break;
            case 2:
                palIndex.Visible = false;
                palItem.Visible = false;
                palBBsDetail.Visible = true;
                GetBBSMsgInfo();
                break;
        }
        GetAncestralHallModel();
    }




    public void GetBBSMsgInfo()
    {
        string strWhere = "id=" + this.BBSMsgId;
        msgModel= bbsBll.GetModel(strWhere);
        string cmdText="select UserName from Users where Userid={0}";
        cmdText =string.Format(cmdText,msgModel.UserId);
        DataTable dataTable=new DataTable();
        dataTable=ExcuteSqlTextBLL.GetDataTable(cmdText);
        if(dataTable.Rows.Count>0)
        {
            userName = dataTable.Rows[0][0].ToString();
        }
    }




    public void ReplyBBSMsg()
    {
        AncestralHallBBSReplyMsgModel replyModel = new AncestralHallBBSReplyMsgModel();
        replyModel.AncestralHallBBSMsgId = BBSMsgId;
        replyModel.Content = txtContent.Text;
        replyModel.PublishTime = DateTime.Now;
        replyModel.UserId = userId;
        replyBll.Add(replyModel);
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        ReplyBBSMsg();
        ResultStr("BBSIndex.aspx?AncestralHallId=" + this.AncestralHallId + "&AncestralHallItemId=" + this.AncestralHallItemId + "&accessType=1", "回帖成功!");
    }

    /// <summary>
    /// 绑定
    /// </summary>
    public void DataBindBBSMsg()
    {
        string cmdText = "select *from AncestralHallBBSMsg where AncestralHallItemId={0}";
        cmdText = string.Format(cmdText, this.AncestralHallItemId);
        DataPagesOperate operate = new DataPagesOperate();
        operate.BindRepeater(cmdText, AspNetPager1, "Id desc", "Id", rptBBSMsg);
        string sourceCmdText = "select count(id) from AncestralHallBBSMsg where AncestralHallItemId={0}";
        sourceCmdText = string.Format(sourceCmdText, this.AncestralHallItemId);
        DataTable sourDataTable = new DataTable();
        sourDataTable = ExcuteSqlTextBLL.GetDataTable(sourceCmdText);
        MsgListCount = StringHelper.isNumber_int(sourDataTable.Rows[0][0].ToString());
    }


    public void GetAncestralHallModel()
    {
        ancestralHallBBSModel = ancestralHallBBSBLL.GetModel("AncestralHallId="+this.AncestralHallId+"and IsDel=0");
        
    }

    public void GetAncestralHallItemModel()
    {
        ancestralHallItemsModel = ancestralHallItemsBLL.GetModel("Id="+this.AncestralHallItemId);

    }

    /// <summary>
    /// 绑定论坛版块
    /// </summary>
    public void DataBindItem()
    {
        string cmdText = "select *from AncestralHallItems where AncestralHallId={0} and IsDel=0";
        cmdText = string.Format(cmdText,this.AncestralHallId);
        DataPagesOperate operate = new DataPagesOperate();
        operate.BindRepeater(cmdText, AspNetPager1, "IsSys desc", "IsSys", rptAncestralHallItems);
        

    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {

    }
    protected void rptAncestralHallItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Label lblMsgCount = e.Item.FindControl("lblMsgCount") as Label;
        Label lblReplyMsgCount = e.Item.FindControl("lblReplyMsgCount") as Label;
        Label lblTodayMsgCount = e.Item.FindControl("lblTodayMsgCount") as Label;

        DataTable topDataTable = new DataTable();
        string topCmdText = "select top 1 *from AncestralHallBBSMsg where AncestralHallItemId={0}";
        topCmdText = string.Format(topCmdText,this.AncestralHallItemId);
        topDataTable = ExcuteSqlTextBLL.GetDataTable(topCmdText);

        Label lblContent = e.Item.FindControl("lblContent") as Label;
        Label lblAuthor = e.Item.FindControl("lblAuthor") as Label;
        Label lblPublishTime = e.Item.FindControl("lblPublishTime") as Label;

        if(topDataTable.Rows.Count>0)
        {
            lblContent.Text = topDataTable.Rows[0]["Title"].ToString();
            lblPublishTime.Text = topDataTable.Rows[0]["PublisTime"].ToString();
            Users_Model usModel = User_Bll.CreateNew.GetModel(StringHelper.isNumber_int(topDataTable.Rows[0]["UserId"].ToString()));
            lblAuthor.Text = usModel.UserName;
        }

        HiddenField hldAncestralHallItemId = e.Item.FindControl("hldAncestralHallItemId") as HiddenField;
        string cmdText = "select count(ab.id) from AncestralHallBBSMsg as ab inner join AncestralHallItems as al on al.Id=ab.AncestralHallItemId where datediff(dd,ab.PublishTime,'" + DateTime.Now.ToString() + "')=0 and al.AncestralHallId=" + this.AncestralHallId + " and al.id=" + hldAncestralHallItemId.Value;
        string cmdTextReplyMsg = "select count(ab.id) from AncestralHallBBSMsg as ab inner join AncestralHallItems as al on al.Id=ab.AncestralHallItemId inner join AncestralHallBBSReplyMsg as ass on ab.id=ass.AncestralHallBBSMsgId where  al.AncestralHallId=" + this.AncestralHallId + " and al.id=" + hldAncestralHallItemId.Value;
        string cmdText2 = "select count(ab.id) from AncestralHallBBSMsg as ab inner join AncestralHallItems as al on al.Id=ab.AncestralHallItemId where datediff(dd,ab.PublishTime,'"+DateTime.Now.ToString()+"')=0 and al.AncestralHallId=" + this.AncestralHallId + " and al.id=" + hldAncestralHallItemId.Value;
        DataTable dataTabl = new DataTable();
        DataTable dataTabl1 = new DataTable();
        DataTable dataTabl2 = new DataTable();
        dataTabl = ExcuteSqlTextBLL.GetDataTable(cmdText);
        dataTabl1 = ExcuteSqlTextBLL.GetDataTable(cmdTextReplyMsg);
        dataTabl2 = ExcuteSqlTextBLL.GetDataTable(cmdText2);
        lblMsgCount.Text = dataTabl.Rows[0][0].ToString();
        lblReplyMsgCount.Text = dataTabl1.Rows[0][0].ToString();
        lblTodayMsgCount.Text = dataTabl2.Rows[0][0].ToString();

    }
    protected void rptBBSMsg_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Label lblReplyCount = e.Item.FindControl("lblReplyCount") as Label;
        HiddenField hldId=e.Item.FindControl("hldId") as HiddenField;
        string cmdText = "select count(*) from AncestralHallBBSReplyMsg where AncestralHallBBSMsgId={0}";
        cmdText = string.Format(cmdText, hldId.Value);
        DataTable dataTable = new DataTable();
        dataTable = ExcuteSqlTextBLL.GetDataTable(cmdText);
        if(dataTable.Rows.Count>0)
        {
            lblReplyCount.Text = dataTable.Rows[0][0].ToString();
        }


    }
}
